<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/json2.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<style type="text/css">
			#provider {
				margin-left: 16%;
			}
		</style>
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
		<div>
			<form id="ff" method="post">
				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr>
						<td width="20%">
							<h id="star">* </h>出库单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_ORDER_NO" name="ORDER_NO" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>销售单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_SALESORDER_ID" name="SALESORDER_ID" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">*</h>用户组
						</td>
						<td width="30%">
							<input class="easyui-combobox" id="input_CUSTOMER_TYPE" name="CUSTOMER_TYPE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>配送单位
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_CUSTOMER_NAME" name="CUSTOMER_NAME" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">* </h>配送时间
						</td>
						<td width="30%">
							<input class="easyui-datebox" id="input_DISTRIBUTION_DATE" name="DISTRIBUTION_DATE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>

					</tr>
				</table>
				<table id="tt">

				</table>
				<div style="padding: 5px 0px ;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" style="border: none;" name="number" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" style="border: none; width: 500px;" id="dx" />
							</td>
						</tr>
					</table>
					<div style="margin-left: 20%;" id="new_footer">

					</div>
				</div>
			</form>
		</div>
	</body>
	<script type="text/javascript">
		var currid; //保存当前页面的id
		var MaxPage; //保存最大id
		var MinPage; //保存最小id
		var id = getID(); //保存获取的ID
		var updataData; //保存深度拷贝后的数据
		var auth = "admin;" + hex_sha1("admin1");
		var isVerify;
		$(document).ready(function() {
			if(id != AddFlag && id != undefined) {
				currid = id;
				loadRemote();
			} else if(id == undefined) {
				currid = AddFlag;
			}
		});

		$('#input_CUSTOMER_TYPE').combobox({
			valueField: 'CUSTOMER_TYPE',
			textField: 'CUSTOMER_TYPE',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + "/logistics/customertype/listall.do",
		});
		$('#input_CUSTOMER_NAME').combobox({
			valueField: 'CUSTOMER_NAME',
			textField: 'CUSTOMER_NAME',
			panelWidth: 200,
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/logistics/customer/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) { //当选中数据后调用该function，并得到该combobox对应的valuefield和textfield，赋值给record
				$('#input_CUSTOMER_TYPE').textbox('setValue', record.CUSTOMER_TYPE);
			} //调用easyui的textbox的写入值方法，将record中name为CUSTOMER_ID的写入id为customer_id的textbox中
		});
		addFooter($("#new_footer"));
		var receipts_type = RECEIPT_SAVE_SYMBOL + RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_VERIFY_SYMBOL;
		receipts_type += RECEIPT_UNVERIFY_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		var _detailToolbarData = AddDetailToolBar(2);
		GetTable();
		getPageMaxAndMin();

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + '/logistics/warehouseout/getmaxminid.do';
			$.ajax({
				dataType: "json",
				type: "get",
				url: _url,
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: 'JSON',
				success: function(result) {
					if(result != null) {
						updataData = deepCopy(result);
						isVerify = result.ISVERIFY;
						updataData.DISTRIBUTION_DATE = updataData.DISTRIBUTION_DATE.replace(/-/g, "/");
						getFormData(updataData);
						currid = updataData.ID;
						var _data = updataData.rows;
						$('#tt').datagrid('loadData', _data);
						verifyStatus(result.ISVERIFY, true);
						CalSum();
					}
				},
				error: function(result) {
					$.messager.alert("消息", "请求失败");
				}
			});
		}

		function loadRemote(_url) {
			var _url = RemoteLinkAddress + '/logistics/warehouseout/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function save() {
			if(isVerify == 1) {
				$.messager.alert("警告", "订单已审核，不能修改");
				return;
			}
			getSaveData();
		}

		function frontpage() {
			if(currid <= MinPage) {
				$.messager.alert("消息", "没有上张");
			} else {
				var _url = RemoteLinkAddress + '/logistics/warehouseout/pre.do?ID=' + currid;
				getRemoteData(_url);
			}
		}

		function nextpage() {
			if(currid >= MaxPage) {
				$.messager.alert("消息", "没有下张");
			} else {
				var _url = RemoteLinkAddress + '/logistics/warehouseout/next.do?ID=' + currid;
				getRemoteData(_url);
			}
		}

		function print() {
			if(isVerify == 0) {
				$.messager.alert("警告", "订单未审核，不能打印");
				return;
			}
			window.open("WareHouseOutOrderPrint.html?ID=" + currid);
		}

		function verifydata() {
			if(isVerify == 1) {
				$.messager.alert("警告", "订单已审核，无须再审");
				return;
			}
			$.messager.confirm("确认", "是否审核", function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/logistics/warehouseout/verify.do", {
						"ID": updataData.ID,
					}, function(result) {
						if(result.msg == "success") {
							$.messager.alert({ // show error message
								title: '成功',
								msg: '审核成功!'
							});
							verifyStatus(1, true);
							isVerify = 1;
						} else {
							$.messager.alert({ // show error message
								title: 'Error',
								msg: result.msg
							});
						}
					}, 'json');
				}
			});
		}

		function unverifydata() {
			if(isVerify == 0) {
				$.messager.alert("警告", "订单已弃审，无须再审");
				return;
			}
			$.messager.confirm('Confirm', '是否弃审', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/logistics/warehouseout/unverify.do", {
							"ID": updataData.ID,
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({
									title: '成功',
									msg: '放弃审核成功!'
								});
								verifyStatus(0, true);
								isVerify = 0;
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function searchmore() {
			AddTabIn("出库单查询", parent.$("#tabs"), "UI/LOGISTICS/WareHouseOutOrder/WareHouseOutOrderSearch.html");
		}

		function GetTable() {
			$('#tt').edatagrid({
				height: 290,
				toolbar: _detailToolbarData,
				collapsible: true,
				singleSelect: true,
				fitColumns: true,
				autoSave: true,
				columns: [
					[{
						field: 'STOREHOUSE_ID',
						title: 'STOREHOUSE_ID',
						width: '0%',
						align: 'center'
					}, {
						field: 'INTERNAL_NAME',
						title: '内部商品名',
						width: '13%',
						align: 'center'
					}, {
						field: 'INTERNAL_TYPE',
						title: '内部商品类型',
						width: '13%',
						align: 'center'
					}, {
						field: 'STOREHOUSE_NAME',
						title: '库房',
						width: '10%',
						align: 'center'

					}, {
						field: 'UNIT_PRICE',
						title: '单价',
						width: '8%',
						align: 'center'
					}, {
						field: 'AMOUNT',
						title: '销售数量',
						width: '12%',
						align: 'center'
					}, {
						field: 'UNIT_NAME',
						title: '原始单位',
						width: '8%',
						align: 'center'
					}, {
						field: 'WAREHOUSEOUT_AMOUNT',
						title: '出库数量',
						width: '12%',
						align: 'center',
						editor: {
							type: 'numberbox'
						}
					}, {
						field: 'INOUT_UNIT',
						title:'单位',
						width:'5%',
						align:'center',
						editor: {
							type: 'textbox',
							options: {
								type: "text",
							}
						},
						formatter: function(value, row, index) {
							if(row.INOUT_UNIT === '' || row.INOUT_UNIT === undefined){
								row.INOUT_UNIT = row.UNIT_NAME
							}
							return row.INOUT_UNIT;
						}
					}, {
						field: 'WAREHOUSEOUT_SUM',
						title: '出库总价',
						width: '13%',
						align: 'center'
					}]
				],
				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					var result = row.UNIT_PRICE * row.WAREHOUSEOUT_AMOUNT;
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							WAREHOUSEOUT_SUM: result.toFixed(2)
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					CalSum();
				}
			});
			$("#tt").datagrid('hideColumn', 'STOREHOUSE_ID');
		}

		function getSaveData() {
			submitFormData(updataData);
			var _data = JSON.stringify(updataData);
			submitData(_data);
		}

		function submitData(_data) {
			var _url = "";
			_url = RemoteLinkAddress + "/logistics/warehouseout/update.do";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _data,
				type: "POST",
				success: function(result) {
					if(result.msg.search("success") > -1) {
						$.messager.alert('提示信息', '修改成功!');
					} else {
						$.messager.alert("提示信息", "修改失败");
					}
				},
				error: function(result) {
					$.messager.alert('提示信息', '修改失败');
				}
			});
		}

		function CalSum() {
			var _num = 0;
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				_num += parseFloat(dataGridRows[i].WAREHOUSEOUT_SUM);
			}
			var num = changeMoneyToChinese(_num);
			$("#dx").val(num);
			$("input[name='number']").val(_num.toFixed(2));
		}

		function exportExcel() {
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						dataType: "json", //服务器返回的数据类型
						url: RemoteLinkAddress + "/logistics/warehouseout/excelorder.do",
						data: {
							"ID": currid
						},
						success: function(result) {
							window.location.href = result.path;
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}
	</script>

</html>